package net.zjitc.servlet.cookiesession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author Administrator
 * @Date 2020/6/2 15:39
 * 验证客户端再次访问服务器时会自动带上cookie
 * 1、获取cookie
 * 2、展示cookie中的内容
 *
 * 浏览器访问验证过程
 * 1、浏览器请求cookieDemo1
 *    创建了cookie，通过响应发送，保存在客户端
 *    2、同一个浏览器请求cookieDemo2
 *       能获取到cookie，浏览器请求时自动带上cookie
 *    3、换一个浏览器fireFox请求cookieDemo2
 *       无法获取这个cookie值，这就验证了cookie是保存在客户端
 *    4、关闭Chrome浏览器，再次打开Chrome浏览器，访问cookieDemo2
 *       也无法获取cookie值，说明cookie默认时临时保存的，没有持久保存
 **/
@WebServlet("/cookieDemo2")
public class CookieDemo2 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*获取cookie并展示内容*/
        Cookie[] cookies = req.getCookies();
        if (cookies!=null) {
            for (int i = 0;i<cookies.length;i++){
                System.out.println(cookies[i].getName()+"/t"+cookies[i].getValue());
            }
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
